package com.xiaonuoxi.handler;

import org.springframework.stereotype.Component;
import org.springframework.web.socket.TextMessage;
import org.springframework.web.socket.WebSocketSession;

import java.io.IOException;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.atomic.AtomicInteger;

/**
 * @Author: 小诺希
 * @Date: 2021-03-26 14:09
 * @Version v1.0
 */
@Component
public class SocketManager {

    //定义 ConcurrentHashMap 存储用户编号和用户会话
    private static ConcurrentHashMap<String, WebSocketSession> manager = new ConcurrentHashMap<String, WebSocketSession>();
    // 在线人数
    private static final AtomicInteger OnlineCount = new AtomicInteger(0);

    /**
     *  添加连接 WebSocketSession
     * @param key
     * @param webSocketSession
     */
    public  void add(String key, WebSocketSession webSocketSession) {
        WebSocketSession webSocketSession1 = manager.get(key);
        if (webSocketSession1 != null){
            try {
                webSocketSession1.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        manager.put(key, webSocketSession);
        OnlineCount.incrementAndGet(); // 在线数加1
    }

    /**
     *  根据key 删除 WebSocketSession
     * @param key
     */
    public  void remove(String key) {
        manager.remove(key);
        OnlineCount.decrementAndGet();
    }

    /**
     *  根据key获取 WebSocketSession 对象
     * @param key
     * @return
     */
    public  WebSocketSession get(String key) {
        return manager.get(key);
    }

    /**
     *  获取连接人数
     * @return
     */
    public  Integer WebsocketActiveNum(){
        return OnlineCount.intValue();
    }

    /**
     *  根据session 移除
     * @param session
     */
    public  void removeBySession(WebSocketSession session){
        //使用泛型
        for (Map.Entry<String, WebSocketSession> entry : manager.entrySet()) {
            if (session.getId().equals(entry.getValue().getId())) {
                // 相等删除
                manager.remove(entry.getKey());
                OnlineCount.decrementAndGet();
            }
        }
    }

    /**
     *  根据key 发送信息
     * @param key
     * @param text
     */
    public void send(String key,String text){
        WebSocketSession webSocketSession1 = manager.get(key);
        if (webSocketSession1 != null && webSocketSession1.isOpen()){
            try {
                webSocketSession1.sendMessage(new TextMessage(text));
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }

    /**
     *  群发
     * @param text
     */
    public void sendToAll(String text){
        for (Map.Entry<String, WebSocketSession> entry : manager.entrySet()) {

            WebSocketSession webSocketSession = entry.getValue();
            if (webSocketSession != null && webSocketSession.isOpen()){
                try {
                    webSocketSession.sendMessage(new TextMessage(text));
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    }

}
